const Router = require('koa-router');
const user= new Router();
const sqldata = require('./sqldata');

user.get('/', async (ctx)=>{
    let val=ctx.request.body;
    let mydata = await new Promise((resolve, reject)=>{
        let sql = `select * from user`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    ctx.body = mydata;
})
user.get('/search', async (ctx)=>{
    let val=ctx.request.query;
    let mydata = await new Promise((resolve, reject)=>{
        let sql = `select * from user where Uphone like '%${val.inp}%'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);  
        })
    })
    ctx.body = mydata;
})
user.delete('/',async(ctx)=>{
    let val=JSON.parse(ctx.request.body);
    console.log('11');
    console.log(val)
    let sql;
    let mydata;
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete from foster where Uid='${val.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete from adopt where Uid='${val.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete pet from pet,user where Uid='${val.Uid}' and user.Pid=pet.Pid`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete attented from attented,user where user.Uid='${val.Uid}' and user.Uid=attented.Uid`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete attented from attented,user where user.Uid='${val.Uid}' and user.Uid=attented.Bid`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete attention from attention,user where user.Uid='${val.Uid}' and user.Uid=attention.Uid`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete attention from attention,user where user.Uid='${val.Uid}' and user.Uid=attention.Bid`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete talking from user,talking where user.Uid='${val.Uid}' and talking.Uid=user.Uid`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete talking from user,talking where user.Uid='${val.Uid}' and talking.Bid=user.Uid`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete workfabulous from workfabulous,user where user.Uid='${val.Uid}' and workfabulous.Uid=user.Uid`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete commentfabulous from commentfabulous,user where user.Uid='${val.Uid}' and commentfabulous.Uid=user.Uid`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete from comment where Uid='${val.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete from works where Uid='${val.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete from user where Uid='${val.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
})

module.exports = user;